package com.demo.Main.StageOne;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

public class Demotest {



//public class demo2 {
//    public static void main(String[] args) {
//        try {
//            Socket socket = new Socket("www.kugou.com", 80);
//            DataOutputStream out = new DataOutputStream(socket.getOutputStream());
//
//            out.writeBytes("GET / HTTP/1.1\r\n");
//            //out.writeBytes("Host: www.baidu.com\r\n");
//            out.writeBytes("Connection: close\r\n\r\n");
//            out.writeBytes("User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36\r\n");
//
//            out.flush();
//            //out.close();
//            BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
//            String inputLine;
//            StringBuilder html = new StringBuilder();
//
//            while ((inputLine = in.readLine()) != null) {
//                html.append(inputLine);
//            }
//
//            in.close();
//            socket.close();
//            System.out.println(html.toString());
//
//            // System.out.println(html.substring(9,12));
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//    }
//}



        public static void main(String[] args) {
            File file = new File("localfile/server.txt");
            try (InputStream inputStream = new FileInputStream(file);
                 BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);) {
                byte[] bytes = bufferedInputStream.readAllBytes();
                String string = new String(bytes);
                String[] line = string.split("\n");

                for (String s : line)
                    try {
                        url(s);
                    } catch (Exception e) {
                        System.out.println("Failed to process URL: " + s);
                        e.printStackTrace();
                    }
            } catch (FileNotFoundException e) {
                throw new RuntimeException(e);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }

        public static void url(String str) {
            try {
                URL url = new URL(str);
                HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                connection.setRequestMethod("GET");
                connection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 10.0; " +
                        "Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36");

                // 获取响应状态码
                int responseCode = connection.getResponseCode();

                if (responseCode == 200) {
                    System.out.println("\033[32;4m" + url + " Code: " + responseCode + "\033[0m");
                } else {
                    System.out.print("\033[31;4m" + url + " Code: " + responseCode + "\033[0m");
                    System.out.println("\033[31;4m" + "====" + "连接失败！！！" + "\033[0m");
                }

                BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
                String inputLine;
                StringBuilder html = new StringBuilder();
                while ((inputLine = in.readLine()) != null) {
                    html.append(inputLine);
                }
                in.close();
                connection.disconnect();

            } catch (MalformedURLException e) {
                throw new RuntimeException(e);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }


}
